package com.zlc.workflow.core.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.core.process.mapper.StoneSystemMapper;
import com.zlc.common.core.process.model.vo.StoneSystemVo;
import com.zlc.common.core.process.service.StoneSystemService;
import com.zlc.common.model.process.system.SystemRequestParam;
import com.zlc.common.util.AssertUtils;
import com.zlc.workflow.core.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SystemServiceImpl implements SystemService {

    @Autowired
    private StoneSystemMapper stoneSystemMapper;
    @Autowired
    private StoneSystemService stoneSystemService;

    @Override
    public PageVo<StoneSystemVo> queryList(SystemRequestParam requestParam) {
        PageHelper.startPage(requestParam.getPageNum(),requestParam.getPageSize());
        List<StoneSystemVo> systemVoList = stoneSystemMapper.queryList(requestParam);
        return PageVo.page(systemVoList);
    }

    @Override
    public void add(StoneSystemVo systemVo) {
        String systemCode = systemVo.getSystemCode();
        StoneSystemVo system = stoneSystemService.getBySystemCode(systemCode);
        AssertUtils.isTrue(ObjectUtils.isEmpty(system),"系统标识已存在，请修改！");
        stoneSystemService.saveVo(systemVo);
    }

    @Override
    public void save(StoneSystemVo systemVo) {
        String systemCode = systemVo.getSystemCode();
        StoneSystemVo system = stoneSystemService.getBySystemCode(systemCode);
        AssertUtils.isTrue(ObjectUtils.isEmpty(system) || system.getId().equals(systemVo.getId()),"系统标识已存在，请修改！");
        stoneSystemService.saveVo(systemVo);
    }

    @Override
    public void remove(String id) {
        stoneSystemService.removeById(id);
    }

    @Override
    public void test() {
        Thread thread = Thread.currentThread();
        System.out.println(thread.getState());
        new Thread(()->{
            System.out.println(thread.getState());

            while(!Thread.State.WAITING.equals(thread.getState())){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Thread.State state = thread.getState();
            System.out.println(state);
        }).start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
